package com.neusoft.di.dibuy.controller;


import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.netty.http.server.HttpServer;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class MyRestController {

    public static void main(String[] args) throws IOException {
        HttpHandler handler = (request,    response) -> {
            HttpHeaders headers = response.getHeaders();
            headers.set("Content-Type","text/plain;charset=utf-8");
            DataBufferFactory  factory = response.bufferFactory();
            DataBuffer buffer = factory.wrap("Hello World!".getBytes(StandardCharsets.UTF_8));
            System.out.println(  request.getURI()  );
            return   response.writeWith(  Mono.just(buffer)  );
        };
//        启动服务侦听。。。。。
        ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
        HttpServer.create().host("127.0.0.1").port(8080).handle(adapter).bindNow();


        System.out.println("server start");

         System.in.read();


    }

}